from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from myadmin import models
from django.core.urlresolvers import reverse
from django.contrib.auth.hashers import make_password, check_password
from project_1.settings import BASE_DIR
import json

# 主页
def  index(request):
	try:
		uid = request.session.get('vipUser')['id']
		car = models.Cart.objects.filter(uid=uid)
		return render(request,'myhome/cart.html',{"data":car})
	except:
		return HttpResponse("<script>alert('请登录');location.href='/myhome/login'</script>")
	
# 金钱计算
def money(request):
	gid = request.GET.get('id')
	# 根据商品id获取商品数据
	mon = models.Goods.objects.get(id=gid)
	# 获取数量
	num = request.GET.get('num')
	# 计算小计
	sums = int(num)*mon.price
	return HttpResponse("%.2f"%(sums))

# 提交
def submit(request):
	try:
		uid = request.session.get('vipUser')['id']
	except:
		return HttpResponse("error")
	# 数量
	num=request.GET.get("num").split(",")
	# 商品id
	goodsid=request.GET.get("goodsid").split(",")
	# 商品id与数量一一对应
	dic = {"id":goodsid,"num":num}
	return HttpResponse(reverse("myhome_place_order")+"?data="+str(dic))

# 删除购物车商品
def delete(request):
	# 根据商品id找到商品
	cid = request.GET.get("caid")
	carts = models.Cart.objects.get(id=int(cid))
	# 删除
	carts.delete()
	return HttpResponse(1)


